package controller;

import entity.NegoRoom;
import entity.NegoStatue;
import util.Tuple2;

import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class RoomController {

    private List<NegoRoom> rooms;

    public RoomController(List<NegoRoom> rooms){
        this.rooms = rooms;
    }

    public int getAmoutOfRooms(){
        return rooms.size();
    }

    /**
     *创建一个新的协商房间
     * @param socket
     * @return 房间号
     */
    public int createRoom(Socket socket){
        int roomNum = 0;
        int amountOfRooms = this.getAmoutOfRooms();
        if(amountOfRooms==0){
            NegoRoom room = new NegoRoom(0);
            room.addClient(socket);
            rooms.add(room);
            roomNum = 0;
        }else{
            int lastRoomNum = rooms.get(amountOfRooms-1).getRoomNum();
            if((amountOfRooms - 1)==lastRoomNum){
                roomNum = lastRoomNum+1;
                NegoRoom room = new NegoRoom(roomNum);
                room.addClient(socket);
                rooms.add(room);
            }else{
                for(int i = 0; i<amountOfRooms; i++){
                    if(i!=rooms.get(i).getRoomNum()){
                        NegoRoom room = new NegoRoom(i);
                        room.addClient(socket);
                        rooms.add(i, room);
                        roomNum = i;
                        break;
                    }else{
                        continue;
                    }
                }
            }
        }

        return roomNum;
    }

    /**
     * 添加客户端到特定的房间
     * @param roomNum
     * @param client
     * @return 该客户端在该房间的编号
     */
    public int addClientToRoom(int roomNum, Socket client){
        int clientNum = 0;
        clientNum = getRoomByRoomNum(roomNum).connect(client);
        //clientNum = rooms.get(roomNum).connect(client);
        return clientNum;
    }



    public Tuple2<Integer, Integer> addClient(Socket client){
        Tuple2<Integer, Integer> tuple2 = new Tuple2<>();
        int roomNum = findRoomNumByClient(client);
        int clientNum = 0;
        if(roomNum == -1){
            List<NegoRoom> preNegoRooms = getPreNegoRooms();
            if(preNegoRooms.isEmpty()){
                roomNum = createRoom(client);
                clientNum = 0;
            }else{
                roomNum = preNegoRooms.get(0).getRoomNum();
                clientNum = addClientToRoom(roomNum, client);
            }
        }else{
            clientNum = addClientToRoom(roomNum, client);
        }
        tuple2.set1(roomNum);
        tuple2.set2(clientNum);
        return tuple2;
    }

    public void clientExit(Socket client){
        int roomNum = findRoomNumByClient(client);
        //System.out.println("roomNum:  "+roomNum);
        NegoRoom room = getRoomByRoomNum(roomNum);
        int clientNum = room.getClientNum(client);
        //System.out.println("clientNum:   "+client);
        room.clientExit(client);
        if(room.getOnlineClients()<=0){
            rooms.remove(room);
        }
    }

    /**
     * 查找某个客户端所在的房间号
     * @param client
     * @return 房间号
     */
    public int findRoomNumByClient(Socket client){
        int roomNum = -1;
        for(NegoRoom room: rooms){
            if(room.isClientInThisRoom(client)){
                roomNum = room.getRoomNum();
                break;
            }
        }
        return roomNum;
    }

    private List<NegoRoom> getPreNegoRooms(){
        List<NegoRoom> preNegoRooms = new ArrayList<>();
        for(NegoRoom room: rooms){
            if(room.getNegoStatue()== NegoStatue.PreNego){
                preNegoRooms.add(room);
            }
        }
        return preNegoRooms;
    }

    public NegoRoom getRoomByRoomNum(int roomNum){
        NegoRoom room = null;
        for(NegoRoom item: rooms){
            if(roomNum==item.getRoomNum()){
                room = item;
            }
        }
        return room;
    }
}
